-- Btext, a text creator using blocks for Minetest   
--	Copyright (C) 2018 Neoniet neoniet AT neoniet.com
--
--	This program is free software: you can redistribute it and/or modify
--	it under the terms of the GNU General Public License as published by
--	the Free Software Foundation, either version 3 of the License, or
--	(at your option) any later version.
--	
--	This program is distributed in the hope that it will be useful,
--	but WITHOUT ANY WARRANTY; without even the implied warranty of
--	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--	GNU General Public License for more details.
--	
--	You should have received a copy of the GNU General Public License
--	along with this program.  If not, see <https://www.gnu.org/licenses/>.
--

local S = btext.intllib
minetest.register_chatcommand("btext",
{
	params = "",
	description = S("It print text in the world using blocks. Start at the position the player is looking at, plus a block"),
	func = function(name, param)
		
		-- Check privs 
		local can_use = minetest.check_player_privs(name, {server=true})
		if can_use == false then
			return false, S("Your do not have privileges to use this command")
		end

		local player = minetest.get_player_by_name(name)
		local player_position = player:get_pos()
		local initial_position = player_position
		local tmp_pos = initial_position
		local degrees = player:get_look_horizontal()*180/math.pi

		local fg = player:get_attribute("btext_fg")
		local bg = player:get_attribute("btext_bg")

		if fg == nill then fg = "default:mese" end
		if bg == nill then bg = "air" end

		btext.printString(tmp_pos,degrees,param,fg,bg)
		
		return true, S("Writting: ").. param .."\n " 
	end,
})

minetest.register_chatcommand("btsetbg",
{
	params = "",
	description = S("Set the block to use in the background"),
	func = function(name, params)
		
		-- Check privs 
		local can_use = minetest.check_player_privs(name, {server=true})
		if can_use == false then
			return false, S("Your do not have privileges to use this command")
		end

		local player = minetest.get_player_by_name(name)
		player:set_attribute("btext_bg",params)
		
		return true 
	end,
})	

minetest.register_chatcommand("btsetfg",
{
	params = "",
	description = S("Set the block to use in the foreground"),
	func = function(name, params)

		-- Check privs 
		local can_use = minetest.check_player_privs(name, {server=true})
		if can_use == false then
			return false, S("Your do not have privileges to use this command")
		end

		local player = minetest.get_player_by_name(name)
		player:set_attribute("btext_fg",params)
		
		return true 
	end,
})

minetest.register_chatcommand("btinfo",
{
	params = "",
	description = S("Print state of bt"),
	func = function(name, params)
		
		-- Check privs 
		local can_use = minetest.check_player_privs(name, {server=true})
		if can_use == false then
			return false, "Your do not have privileges to use this command"
		end

		local player = minetest.get_player_by_name(name)
		local fg = player:get_attribute("btext_fg")
		local bg = player:get_attribute("btext_bg")
	
		if fg == nill then fg = "default:mese" end
		if bg == nill then bg = "air" end	

		return true, fg .. " " .. bg .. "\n" 
	end,
})
